package com.zenithmind.article.controller;

import com.zenithmind.common.annotation.Log;
import com.zenithmind.common.constant.OperationTypeEnum;
import com.zenithmind.article.pojo.domain.Category;
import com.zenithmind.article.pojo.query.CategoryQuery;
import com.zenithmind.article.service.CategoryService;
import com.zenithmind.common.result.PageResult;
import com.zenithmind.common.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 文章分类控制器
 * 遵循单一职责原则：专注于分类管理
 * 遵循依赖倒置原则：通过构造器注入依赖
 */
@RestController
@RequestMapping("/api/zenithMind/article-category")
@Tag(name = "分类管理")
public class ArticleCategoryController {

    private final CategoryService categoryService;

    // 构造器注入，遵循依赖倒置原则
    public ArticleCategoryController(CategoryService categoryService) {
        this.categoryService = categoryService;
    }
    
    @Log(description = "获取分类列表", operationType = OperationTypeEnum.QUERY)
    @Operation(summary = "获取分类列表")
    @GetMapping("/categoryList")
    public Result<List<Category>> categoryList() {
        List<Category> categories = categoryService.list();
        return Result.success(categories);
    }

    @Log(description = "获取分类详情", operationType = OperationTypeEnum.QUERY)
    @Operation(summary = "获取分类详情")
    @GetMapping("/categoryDetail/{id}")
    public Result<Category> categoryDetail(@PathVariable String id) {
        Category category = categoryService.getById(id);
        return Result.success(category);
    }

    @Log(description = "添加或更新分类", operationType = OperationTypeEnum.UPDATE)
    @Operation(summary = "添加或更新分类")
    @PostMapping("/addOrUpdateCategory")
    public Result<Category> addOrUpdateCategory(@RequestBody Category category) {
        Category addedOrUpdateCategory = categoryService.addOrUpdateCategory(category);
        return Result.success(addedOrUpdateCategory);
    }
    
    @Log(description = "批量删除分类", operationType = OperationTypeEnum.DELETE)
    @Operation(summary = "批量删除分类")
    @DeleteMapping("/deleteCategory")
    public Result<List<Category>> deleteCategory(@RequestBody List<String> ids) {
        categoryService.removeByIds(ids);
        return Result.success();
    }

    @Log(description = "分页查询分类", operationType = OperationTypeEnum.QUERY)
    @Operation(summary = "分页查询分类")
    @GetMapping("/pageCategory")
    public Result<PageResult<Category>> pageCategory(CategoryQuery categoryQuery) {
        PageResult<Category> categories = categoryService.pageCategory(categoryQuery);
        return Result.success(categories);
    }

}
